package com.restfinder.restaurant.service;

import java.io.Serializable;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.restfinder.common.dao.IRepository;
import com.restfinder.restaurant.component.GeolocalizationUtils;
import com.restfinder.restaurant.dao.DatabaseRestaurantRepository;
import com.restfinder.restaurant.dao.RestaurantRepository;
import com.restfinder.restaurant.model.Restaurant;

@Service
@Transactional(rollbackFor=Exception.class)
public class RestaurantService implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private RestaurantRepository restaurantIRepository;
	
	public Restaurant getById(long id){
		return restaurantIRepository.getById(id);
	}
	
	public void saveOrUpdate(Restaurant restaurant) throws Exception{
		Map<String, Double> geolocalizationMap = GeolocalizationUtils.getLocalization(restaurant.getAddress());
		restaurant.setLatitude(geolocalizationMap.get(GeolocalizationUtils.LATTITUDE_MAP_KEY));
		restaurant.setLongitude(geolocalizationMap.get(GeolocalizationUtils.LONGITUDE_MAP_KEY));
		restaurantIRepository.saveOrUpdate(restaurant);
	}

	public void update(Restaurant restaurant) {
		restaurantIRepository.update(restaurant);
	}

}
